package com.ayf.payment.game.api.controller;

import com.ayf.payment.game.api.dto.GroupsDTO;
import com.ayf.payment.game.api.dto.MerchantDTO;
import com.ayf.payment.game.api.dto.criteria.GroupsSelectCriteria;
import com.ayf.payment.game.api.entity.Groups;
import com.ayf.payment.game.api.entity.platform.NetRechargeUrl;
import com.ayf.payment.game.api.exception.BusinessException;
import com.ayf.payment.game.api.service.GroupsService;
import com.ayf.payment.game.api.service.ProxyNetConfigService;
import com.ayf.payment.game.api.service.platform.NetRechargeUrlService;
import com.ayf.payment.game.api.type.CommonConstants;
import com.ayf.payment.game.api.utils.CommonResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * 分组管理
 *
 * @author wwl
 * @date 2019/12/5 13:39
 */
@RequestMapping("/v/groups")
@RestController
public class GroupsController {

    @Autowired
    private GroupsService groupsService;

    @Autowired
    private NetRechargeUrlService netRechargeUrlService;

    @Autowired
    private ProxyNetConfigService proxyNetConfigService;

    /**
     * 查询分组列表-开区助手专用
     *
     * @param merchantDTO
     * @return
     */
    @PostMapping("/selectAll")
    public CommonResult<List<GroupsDTO>> selectGroupsDto(MerchantDTO merchantDTO) {
        List<GroupsDTO> list = groupsService.selectByDTO(merchantDTO.getId());
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, CommonConstants.OPERATION_SUCCESS, list);
    }

    /**
     * 查询分组列表-商户后台专用
     *
     * @param merchantDTO
     * @return
     */
    @PostMapping("/selectAll2")
    public CommonResult<List<GroupsDTO>> selectGroupsDto2(MerchantDTO merchantDTO,@RequestBody GroupsDTO groupsDTO) {
        String url = "";
        List<NetRechargeUrl> urls = netRechargeUrlService.selectAll();
        if (urls != null && urls.size() > 0) {
            url= urls.get(0).getUrl() + "/gws/checkGroup.html";
        }
        groupsDTO.setGroupUrl(url);
        groupsDTO.setMerchantId(merchantDTO.getId());
        List<GroupsDTO> list = groupsService.selectList(groupsDTO);
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, CommonConstants.OPERATION_SUCCESS, list);
    }

    /**
     * 查找分组用户
     *
     * @param merchantDTO
     * @param criteria
     * @return
     */
    @PostMapping("/find")
    public CommonResult<GroupsDTO> findEntity(MerchantDTO merchantDTO, @RequestBody GroupsSelectCriteria criteria) {

        if (StringUtils.isEmpty(criteria.getGroupsId())) {
            throw new BusinessException(CommonConstants.RETURN_ERROR, "ID 为空");
        }
        GroupsDTO entity = groupsService.findDTO(merchantDTO.getId(), criteria.getGroupsId());
        return new CommonResult<GroupsDTO>(CommonConstants.RETURN_SUCCESS, CommonConstants.OPERATION_SUCCESS, entity);
    }

    /**
     * 保存分组
     *
     * @param entity
     * @return
     */
    @PostMapping("/save")
    public CommonResult<String> saveEntity(MerchantDTO merchantDTO, @RequestBody Groups entity) {
        if (StringUtils.isEmpty(entity.getGroupName())) {
            throw new BusinessException(CommonConstants.RETURN_ERROR, "组名为空");
        }
        entity.setMerchantId(merchantDTO.getId());
        groupsService.save(entity);
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, CommonConstants.OPERATION_SUCCESS, "");
    }

    /**
     * 更新分组
     *
     * @param entity
     * @return
     */
    @PostMapping("/update")
    public CommonResult<String> updateEntity(MerchantDTO merchantDTO, @RequestBody Groups entity) {
        if (StringUtils.isEmpty(entity.getId())) {
            throw new BusinessException(CommonConstants.RETURN_ERROR, "参数异常");
        }
        entity.setMerchantId(merchantDTO.getId());
        entity.setLastUpdateTime(new Date());
        groupsService.update(entity);

        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, CommonConstants.OPERATION_SUCCESS, "");
    }

    /**
     * 删除分组
     *
     * @param groupsDTO
     * @return
     */
    @PostMapping("/del")
    public CommonResult<String> delEntity(@RequestBody GroupsDTO groupsDTO) {
        groupsService.delete(groupsDTO);
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, CommonConstants.OPERATION_SUCCESS,"");
    }


}
